package com.example.wastes.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wastes.common.pojo.Shopping;
import com.example.wastes.shopping.mapper.ShoppingMapper;
import com.example.wastes.shopping.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class ShoppingServiceImpl extends ServiceImpl<ShoppingMapper,Shopping> implements ShoppingService {
    @Autowired
    private ShoppingMapper shoppingMapper;
    @Override
    public List<Shopping> findShoppingranking() {
        return shoppingMapper.findShoppingranking();
    }

    @Override
    public List<Shopping> findShopingcondition(int id) {
        return shoppingMapper.findShopingcondition(id);
    }

    @Override
    public int upload(Shopping shopping, MultipartFile fileUpload, HttpServletRequest request) {
        String originalFilename = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新生成文件名字
        originalFilename = UUID.randomUUID()+suffixName;
        //制定本地文件夹存储图片，写道需要保存的目录下
        String filePath = "D:\\ideaworkspeace\\images\\javaText\\";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+originalFilename));
            shopping.setShoppingimg("http://localhost:8080/wastes/" + originalFilename);
            return shoppingMapper.upShopping(shopping);
            //返回提示信息
        }catch(IOException e) {
            e.printStackTrace();
            throw new RuntimeException("头像上传失败");
        }
    }

    @Override
    public Shopping SelectIntegral(Integer id) {
        return shoppingMapper.SelectIntegral(id);
    }

    @Override
    public List<Shopping> findShoppingName(String shoppingname) {
        return shoppingMapper.findShoppingName(shoppingname);
    }

    @Override
    public int shoppingImg(Shopping shopping, MultipartFile fileUpload, HttpServletRequest request) {
        Shopping shopping1 = new Shopping();
        shopping1.setShoppingname(shopping.getShoppingname());
        shopping1.setIntegral(shopping.getIntegral());
        shoppingMapper.insert(shopping1);
        System.out.println(shopping1);

        String originalFilename = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新生成文件名字
        originalFilename = UUID.randomUUID()+suffixName;
        //制定本地文件夹存储图片，写道需要保存的目录下
        String filePath = "D:\\ideaworkspeace\\images\\javaText\\";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+originalFilename));
            shopping1.setShoppingimg("http://localhost:8082/wastes/" + originalFilename);
            return shoppingMapper.upShopping(shopping1);
            //返回提示信息
        }catch(IOException e) {
            e.printStackTrace();
            throw new RuntimeException("头像上传失败");
        }
    }


}
